package cn.nbt.busManage.controller;

import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.PointDto;
import cn.nbt.busManage.model.entity.Point;
import cn.nbt.busManage.model.vo.PointVo;
import cn.nbt.busManage.service.PointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lyq
 * @time 2024/6/11 18:01
 */

@RestController
@RequestMapping("/point")
public class PointController {

    @Autowired
    private PointService pointService;

    /**
     * 查询所有站点信息
     * @return 所有站点信息
     */
    @GetMapping("/list")
    public ResponseResult<Object> list() {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<PointDto> pointDtoList = pointService.findAll();
            if(pointDtoList == null) {
                result.Error();
                return result;
            }
            ArrayList<PointVo> pointVos = new ArrayList<>();
            for (PointDto pointDto : pointDtoList) {
                pointVos.add(new PointVo(pointDto));
            }
            result.Success("查询成功",pointVos);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }

    /**
     * 根据信息查询站点
     * @param query 查询信息（名称或id）
     * @return 站点信息
     */
    @GetMapping("/findByQuery")
    public ResponseResult<Object> findByQuery(@RequestParam(name = "query") String query) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<PointDto> pointDtoList = pointService.findByQuery(query);
            if (pointDtoList == null) {
                result.BadRequest();
                return result;
            }
            ArrayList<PointVo> pointVos = new ArrayList<>();
            for (PointDto pointDto : pointDtoList) {
                pointVos.add(new PointVo(pointDto));
            }
            result.Success("查询成功",pointVos);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }

    /**
     * 根据id查询站点
     * @param id 站点id
     * @return 站点信息
     */
    @GetMapping("/findById")
    public ResponseResult<Object> findById(@RequestParam(name = "id") Integer id) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            PointDto pointDto = pointService.findById(id);
            if (pointDto == null) {
                result.Error();
                return result;
            }
            PointVo pointVo = new PointVo(pointDto);
            result.Success("查询成功",pointVo);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }

    /**
     * 保存站点信息
     * @param pointVo 站点信息
     */
    @PostMapping("/save")
    public ResponseResult<Object> save(@RequestBody PointVo pointVo) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            String pointName = pointVo.getPointName();
            Point byPointName = pointService.findByPointName(pointName);
            if(byPointName != null) {
                result.BadRequest("失败啦！", "站点名称已存在");
                return result;
            }
            PointDto pointDto = new PointDto(pointVo);
            pointService.save(pointDto);
            result.Success("保存成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }

    /**
     * 删除站点信息
     * @param id 站点id
     */
    @DeleteMapping("/delete")
    public ResponseResult<Object> delete(@RequestParam(name = "id") Integer id) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            pointService.delete(id);
            result.Success("删除成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }

    /**
     * 更新站点信息
     * @param pointVo 站点信息
     */
    @PutMapping("/update")
    public ResponseResult<Object> update(@RequestBody PointVo pointVo) {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            PointDto byId = pointService.findById(pointVo.getId());
            if(!byId.getPointName().equals(pointVo.getPointName())){
                Point byPointName = pointService.findByPointName(pointVo.getPointName());
                if(byPointName != null) {
                    result.BadRequest("失败啦！", "站点名称已存在");
                    return result;
                }
            }
            PointDto pointDto = new PointDto(pointVo);
            pointService.update(pointDto);
            result.Success("更新成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error();
        }
        return result;
    }
}


